package com.jl15988.simpletools.appcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jl15988.simpletools.appcore.dto.ReviewDTO;
import com.jl15988.simpletools.appcore.dto.ReviewRequestDTO;
import com.jl15988.simpletools.appcore.service.ReviewService;
import com.jl15988.simpletools.common.annotation.Log;
import com.jl15988.simpletools.common.constant.OperationType;
import com.jl15988.simpletools.common.utils.Result;
import com.jl15988.simpletools.common.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 评论控制器
 *
 * @author jl15988
 */
@RestController
@RequestMapping("/api/reviews")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 分页查询应用评论
     *
     * @param appId 应用ID
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping
    public Result<IPage<ReviewDTO>> getReviewPage(@RequestParam Long appId,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(reviewService.getReviewPage(appId, pageNum, pageSize));
    }

    /**
     * 创建评论
     *
     * @param request 评论请求DTO
     * @param httpServletRequest HTTP请求
     * @return 评论ID
     */
    @PostMapping
    @Log(title = "创建评论", operationType = OperationType.CREATE)
    public Result<Long> createReview(@Valid @RequestBody ReviewRequestDTO request, HttpServletRequest httpServletRequest) {
        Long userId = TokenUtils.getUserId(httpServletRequest);
        String username = TokenUtils.getUsername(httpServletRequest);
        return Result.success(reviewService.createReview(request, userId, username));
    }

    /**
     * 更新评论
     *
     * @param request 评论请求DTO
     * @param httpServletRequest HTTP请求
     * @return 是否成功
     */
    @PutMapping
    @Log(title = "更新评论", operationType = OperationType.UPDATE)
    public Result<Boolean> updateReview(@Valid @RequestBody ReviewRequestDTO request, HttpServletRequest httpServletRequest) {
        Long userId = TokenUtils.getUserId(httpServletRequest);
        return Result.success(reviewService.updateReview(request, userId));
    }

    /**
     * 删除评论
     *
     * @param id 评论ID
     * @param httpServletRequest HTTP请求
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除评论", operationType = OperationType.DELETE)
    public Result<Boolean> deleteReview(@PathVariable Long id, HttpServletRequest httpServletRequest) {
        Long userId = TokenUtils.getUserId(httpServletRequest);
        return Result.success(reviewService.deleteReview(id, userId));
    }

    /**
     * 获取应用平均评分
     *
     * @param appId 应用ID
     * @return 平均评分
     */
    @GetMapping("/rating")
    public Result<Double> getAverageRating(@RequestParam Long appId) {
        return Result.success(reviewService.getAverageRating(appId));
    }
} 